package cn.iocoder.yudao.module.erp.controller.admin.depplan;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.depplan.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.depplan.DepPlanDO;
import cn.iocoder.yudao.module.erp.service.depplan.DepPlanService;

@Tag(name = "管理后台 - ERP 科室采购计划")
@RestController
@RequestMapping("/erp/dep-plan")
@Validated
public class DepPlanController {

    @Resource
    private DepPlanService depPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP 科室采购计划")
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:create')")
    public CommonResult<Long> createDepPlan(@Valid @RequestBody DepPlanSaveReqVO createReqVO) {
        return success(depPlanService.createDepPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 科室采购计划")
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:update')")
    public CommonResult<Boolean> updateDepPlan(@Valid @RequestBody DepPlanSaveReqVO updateReqVO) {
        depPlanService.updateDepPlan(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 科室采购计划")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:delete')")
    public CommonResult<Boolean> deleteDepPlan(@RequestParam("id") Long id) {
        depPlanService.deleteDepPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 科室采购计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:query')")
    public CommonResult<DepPlanRespVO> getDepPlan(@RequestParam("id") Long id) {
        DepPlanDO depPlan = depPlanService.getDepPlan(id);
        return success(BeanUtils.toBean(depPlan, DepPlanRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 科室采购计划分页")
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:query')")
    public CommonResult<PageResult<DepPlanRespVO>> getDepPlanPage(@Valid DepPlanPageReqVO pageReqVO) {
        PageResult<DepPlanDO> pageResult = depPlanService.getDepPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DepPlanRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 科室采购计划 Excel")
    @PreAuthorize("@ss.hasPermission('erp:dep-plan:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDepPlanExcel(@Valid DepPlanPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DepPlanDO> list = depPlanService.getDepPlanPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 科室采购计划.xls", "数据", DepPlanRespVO.class,
                        BeanUtils.toBean(list, DepPlanRespVO.class));
    }

}